//
//  BCFatherViewController.swift
//  BarCode
//
//  Created by xxcao on 2017/3/29.
//  Copyright © 2017年 MobileArtisan. All rights reserved.
//

import UIKit

class BCFatherViewController: UIViewController {
    var pointI: UIImageView?
    var bgColor = UIColor(red: 245.0 / 255, green: 245.0 / 255, blue: 245.0 / 255, alpha: 1.0)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        // Do any additional setup after loading the view.
        //启用滑动返回（swipe back）
        navigationController?.interactivePopGestureRecognizer!.delegate = self
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    //MARK: - 自定义方法
    func customBackBarItem () {
//        let leftBarBtn = UIBarButtonItem(title: "", style: .plain, target: self,
//                                         action: #selector(BCFatherViewController.backToPrevious))
//        leftBarBtn.image = UIImage(named: "backBtnU")
//        
//        //用于消除左边空隙，要不然按钮顶不到最前面
//        let spacer = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil,
//                                     action: nil)
//        spacer.width = 8
//        navigationItem.leftBarButtonItems = [spacer, leftBarBtn]
        
        let leftBtn = UIButton()
        leftBtn.setImage(UIImage(named: "backBtnU"), for: .normal)
        leftBtn.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
        leftBtn.addTarget(self, action: #selector(backToPrevious), for: .touchUpInside)
        let leftBarBtn = UIBarButtonItem(customView: leftBtn)
        navigationItem.leftBarButtonItem = leftBarBtn
    }
    
    func backToPrevious() {
        navigationController?.popViewController(animated: true)
    }
    
    func customRightBarItem(str: String, imgStr: String) {
        
        let barView = UIView()
//        barView.backgroundColor = UIColor.yellow
        barView.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
        
        let pointImg = UIImageView()
        pointImg.frame = CGRect(x: 40, y: 2, width: 8, height: 8)
        pointImg.layer.cornerRadius = 4
        pointImg.layer.masksToBounds = true
        pointImg.backgroundColor = .red
        barView.addSubview(pointImg)
        pointI = pointImg
        
        
        let rightBtn = UIButton()
        rightBtn.frame = CGRect(x: 10, y: 3, width: 30, height: 30)
        rightBtn.addTarget(self, action: #selector(rightBarItemAction), for: .touchUpInside)
//        rightBtn.backgroundColor = UIColor.blue
        rightBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -4)
        rightBtn.setImage(UIImage(named: imgStr), for: .normal)
        barView.addSubview(rightBtn)
        
        let rightNaviBarItem = UIBarButtonItem(customView: barView)
        navigationItem.rightBarButtonItem = rightNaviBarItem
        
    }
    
    func rightBarItemAction() {
        //subclass override
    }
}


extension BCFatherViewController: UIGestureRecognizerDelegate {
    //是否允许手势
    private func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
        if (gestureRecognizer == self.navigationController?.interactivePopGestureRecognizer) {
            //只有二级以及以下的页面允许手势返回
            return (self.navigationController?.viewControllers.count)! > 1
        }
        return true
    }
}
//MARK: - Custom Navigation
class BCNavigationController: UINavigationController {
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationBar.barTintColor =  UIColor(red: 28.0/255, green: 202.0/255, blue: 147.0/255,
                                              alpha: 1)
        navigationBar.isTranslucent = false
        navigationBar.setBackgroundImage(UIImage(), for: .any, barMetrics: .default)
        navigationBar.shadowImage = UIImage()
        let shadow = NSShadow()
        shadow.shadowColor = UIColor.black
        shadow.shadowOffset = CGSize(width: 0.5, height: 0.5)
        let dic = [NSForegroundColorAttributeName:UIColor.white,
                   NSShadowAttributeName:shadow,
                   NSFontAttributeName: UIFont.boldSystemFont(ofSize: 20)]
        navigationBar.titleTextAttributes = dic
        
        navigationBar.tintColor = .white
    }
}
